package persistency;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import model.Product;

public class Product_CRUD implements Repository<Product> {

	//private static final String PERSISTENCE_UNIT_NAME = "MiniServiceDesk";
	
	@Override
	public void insert(Product entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.persist(entity);
			em.flush();
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
	}

	@Override
	public void update(Product entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.merge(entity);
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
	}

	@Override
	public void delete(Product entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Product dProduct = getById(entity.getProductId());
			em.remove(dProduct);
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getAll() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Product> allProducts = em.createQuery(
					"Select p from Product p").getResultList();
			tx.commit();
			return allProducts;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Product> getAllActive() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Product> allProducts = em.createQuery(
					"Select p from Product p where active = true")
					.getResultList();
			tx.commit();
			return allProducts;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	@Override
	public Product getById(Object id) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Product gProduct = em.find(Product.class, id);
			tx.commit();
			return gProduct;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	public Product getById(String id) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Product gProduct = em.find(Product.class, Long.parseLong(id));
			tx.commit();
			return gProduct;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	@Override
	public int getCount() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			int pCount = Integer.parseInt(em
					.createQuery("Select count(p.productId) from Product p")
					.getSingleResult().toString());
			tx.commit();
			return pCount;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return 0;
	}
}
